package com.example.myapplication;

import static androidx.core.content.ContextCompat.startActivity;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class Myadapter2 extends RecyclerView.Adapter<Myadapter2.Viewholder> {
    private List<Album> list1;

    Context context1;
    // 传入值
    public Myadapter2(List list,Context context) {
        list1=list;
        context1=context;
    }


    @NonNull
    @Override
    public Viewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 压缩器（from生成静态压缩器）
        // 闪退时可以挂布尔值false运行
        View view=LayoutInflater.from(context1).inflate(R.layout.item1,parent,false);
        Viewholder viewholder=new Viewholder(view);
        return viewholder;
    }

    @Override
    public void onBindViewHolder(@NonNull Viewholder holder, int position) {
        Album album=list1.get(position);
        // （fragment1中显示）获取专辑的名称，封面图片
        holder.textView.setText(album.getName());
        holder.imageView.setImageResource(album.getImageid());

        // 点击专辑图片，跳转并查看详情页面
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context1,Activityvx.class);
                // 向activityvx传入专辑名和主打曲
                intent.putExtra("name", album.getName());
                intent.putExtra("song", album.getSong());
                context1.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list1.size();
    }

    // 内嵌类 holder类似一个行指针
    static class Viewholder extends RecyclerView.ViewHolder{
        View view;
        // 存放专辑名称
        TextView textView;
        // 存放专辑封面
        ImageView imageView;

        public Viewholder(@NonNull View itemView) {
            super(itemView);
            view=itemView;
            // item1.xml中的控件对象
            textView=itemView.findViewById(R.id.textView4);
            imageView=itemView.findViewById(R.id.imageView);
        }
    }
}
